package ed.hbut.supermarket1.controller;

import ed.hbut.supermarket1.dto.MerchandiseDto;
import ed.hbut.supermarket1.dto.SaleDto;
import ed.hbut.supermarket1.entity.Merchandise;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.entity.Sale;
import ed.hbut.supermarket1.service.SaleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class SaleController {

    @Autowired
    public SaleService saleService;


    /**
     * 查询订单的基本信息
     * @return
     */
    @GetMapping("/api/sales")
    public Result sale() {


         log.info("查询订单请求");
         List<Sale> list = saleService.getAllSale();
        return Result.success(list);
    }


    /**
     * 根据id查询该订单的商品记录
     * @param id
     * @return
     */
    @GetMapping("/api/sales/{id}")
    public Result saleById(@PathVariable int id) {

        log.info("根据id查询订单商品:{}",id);
        List<MerchandiseDto> list = saleService.getMerchandiseBySaleId(id);
        log.info("查询的商品列表:{}",list);
        return Result.success(list);
    }


    /**
     * 新增订单
     * @param saleDto
     * @return
     */
   @PostMapping("/api/sales")
        public Result sale(@RequestBody SaleDto saleDto) {

       log.info("生成订单请求:{}", saleDto);
       saleService.addSale(saleDto);

       return Result.success();
   }


   @DeleteMapping("/api/sales/{id}")
    public Result deleteSale(@PathVariable int id) {

       log.info("删除订单:{}",id);
       saleService.deleteSale(id);

       return Result.success();
   }

}
